﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Resolvers;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;

namespace EquationsBankReader.EquationsRead
{
    class XML_Parser : XDocument
    {
        #region Class variables

        public  XDocument EquationFile;
        public  UInt32    Equation_Number;
        public  UInt32    Path_Number;
        public  UInt32    Name_Number;
        public  UInt32    Formula_Number;
        public  UInt32    Formula_Variable_Number;
        public  UInt32    Reference_Number;
        public  UInt32    Citation_Number;
        public  UInt32    Equation_Description_Number;

        #endregion

        public XML_Parser(FileInfo InputFile)
        {
            EquationFile                    = XDocument.Load(InputFile.FullName);
            Path_Number                     = 1;
            Equation_Number                 = 1;
            Name_Number                     = 1;
            Formula_Number                  = 1;
            Formula_Variable_Number         = 1;
            Reference_Number                = 1;
            Citation_Number                 = 1;
            Equation_Description_Number     = 1;
        }

        #region Variables

        #region Settings

        // Get the Settings node
        public XElement Settings
        {
            get
            {
                return Equation.Element("Settings");
            }
        }

        // Get the Settings node
        public string Settings_Value
        {
          get
          {
            return Equation.Element("Settings").Name.ToString();
          }
        }

        // <!--Enable Solver-->
        public XElement V_Equation_00600
        {
          get
          {
            return Settings.Element("V_Equation_00600");
          }
        }

        // <!--V_Equation_00600 value-->
        public string V_Equation_00600_Value
        {
          get
          {
            return Settings.Element("V_Equation_00600").Attribute("Value").Value;
          }
          set
          {
            Settings.Element("V_Equation_00600").SetAttributeValue("Value", value);
          }
        }
        
        #endregion

        #region First node element

        // Get and set the main node in the file
        public XElement Equation
        {
            get
            {
                foreach (XElement Current_Equation in EquationFile.Elements("Equation"))
                {
                    if (Current_Equation.Attribute("Value").Value.ToString() == Equation_Number.ToString())
                    {
                        return Current_Equation;
                    }
                }
                return null;
            }
        }

        #endregion

        #region Path

        // <!--Path-->
        public XElement V_Equation_00001
        {
            get
            {
                foreach (XElement Current_V_Equation_00001 in Equation.Elements("V_Equation_00001"))
                {
                    if (Current_V_Equation_00001.Attribute("Value").Value.ToString() == Path_Number.ToString())
                    {
                        return Current_V_Equation_00001;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00001 value-->
        public string V_Equation_00001_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00001 in Equation.Elements("V_Equation_00001"))
                {
                    if (Current_V_Equation_00001.Attribute("Value").Value.ToString() == Path_Number.ToString())
                    {
                        return Current_V_Equation_00001.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00001 in Equation.Elements("V_Equation_00001"))
                {
                    if (Current_V_Equation_00001.Attribute("Value").Value.ToString() == Path_Number.ToString())
                    {
                        Current_V_Equation_00001.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--Path for equation to be displayed in English-->
        public XElement V_Equation_00002
        {
            get
            {
                return V_Equation_00001.Element("V_Equation_00002");
            }
        }

        // <!--V_Equation_00002 value-->
        public string V_Equation_00002_Value
        {
            get
            {
                return V_Equation_00001.Element("V_Equation_00002").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00001.Element("V_Equation_00002").SetAttributeValue("Value", value);
            }
        }

        // <!--Path for equation to be displayed in German-->
        public XElement V_Equation_00003
        {
            get
            {
                return V_Equation_00001.Element("V_Equation_00003");
            }
        }

        // <!--V_Equation_00002 value-->
        public string V_Equation_00003_Value
        {
            get
            {
                return V_Equation_00001.Element("V_Equation_00003").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00001.Element("V_Equation_00003").SetAttributeValue("Value", value);
            }
        }

        // List Path langauges.
        public List<string[]> GetPathLanguages(XElement v_equation_00001)
        {
            List<string[]> langs = new List<string[]>();

            foreach (var desc in v_equation_00001.Descendants())
            {
                string[] lang = { desc.Name.LocalName, desc.FirstAttribute.Value };
                langs.Add(lang);
            }

            return langs;
        }

        #endregion

        #region Equation name

        // <!--Equation name-->
        public XElement V_Equation_00100
        {
            get
            {
                foreach (XElement Current_V_Equation_00100 in Equation.Elements("V_Equation_00100"))
                {
                    if (Current_V_Equation_00100.Attribute("Value").Value.ToString() == Name_Number.ToString())
                    {
                        return Current_V_Equation_00100;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00100 value-->
        public string V_Equation_00100_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00100 in Equation.Elements("V_Equation_00100"))
                {
                    if (Current_V_Equation_00100.Attribute("Value").Value.ToString() == Name_Number.ToString())
                    {
                        return Current_V_Equation_00100.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00100 in Equation.Elements("V_Equation_00100"))
                {
                    if (Current_V_Equation_00100.Attribute("Value").Value.ToString() == Name_Number.ToString())
                    {
                        Current_V_Equation_00100.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--English name-->
        public XElement V_Equation_00101
        {
            get
            {
                return V_Equation_00100.Element("V_Equation_00101");
            }
        }

        // <!--V_Equation_00101 value-->
        public string V_Equation_00101_Value
        {
            get
            {
                return V_Equation_00100.Element("V_Equation_00101").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00100.Element("V_Equation_00101").SetAttributeValue("Value", value);
            }
        }

        // <!--German name-->
        public XElement V_Equation_00102
        {
            get
            {
                return V_Equation_00100.Element("V_Equation_00102");
            }
        }

        // <!--V_Equation_00102 value-->
        public string V_Equation_00102_Value
        {
            get
            {
                return V_Equation_00100.Element("V_Equation_00102").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00100.Element("V_Equation_00102").SetAttributeValue("Value", value);
            }
        }

        // List Name langauges.
        public List<string[]> GetNameLanguages(XElement v_equation_00100)
        {
            List<string[]> langs = new List<string[]>();

            foreach (var desc in v_equation_00100.Descendants())
            {
                string[] lang = { desc.Name.LocalName, desc.FirstAttribute.Value };
                langs.Add(lang);
            }

            return langs;
        }

        #endregion

        #region Formula

        // <!--Formula-->
        public XElement V_Equation_00200
        {
            get
            {
                foreach (XElement Current_V_Equation_00200 in Equation.Elements("V_Equation_00200"))
                {
                    if (Current_V_Equation_00200.Attribute("Value").Value.ToString() == Formula_Number.ToString())
                    {
                        return Current_V_Equation_00200;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00200 value-->
        public string V_Equation_00200_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00200 in Equation.Elements("V_Equation_00200"))
                {
                    if (Current_V_Equation_00200.Attribute("Value").Value.ToString() == Formula_Number.ToString())
                    {
                        return Current_V_Equation_00200.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00200 in Equation.Elements("V_Equation_00200"))
                {
                    if (Current_V_Equation_00200.Attribute("Value").Value.ToString() == Formula_Number.ToString())
                    {
                        Current_V_Equation_00200.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--Value of the equation formula-->
        public XElement V_Equation_00201
        {
            get
            {
                return V_Equation_00200.Element("V_Equation_00201");
            }
        }

        // <!--V_Equation_00201 value-->
        public string V_Equation_00201_Value
        {
            get
            {
                return V_Equation_00200.Element("V_Equation_00201").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00200.Element("V_Equation_00201").SetAttributeValue("Value", value);
            }
        }

        // <!--Variable in the equation formula(V_Equation_00201)-->
        public XElement V_Equation_00202
        {
            get
            {
                foreach (XElement Current_V_Equation_00202 in V_Equation_00200.Elements("V_Equation_00202"))
                {
                    if (Current_V_Equation_00202.Attribute("Value").Value.ToString() == Formula_Variable_Number.ToString())
                    {
                        return Current_V_Equation_00202;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00202 value-->
        public string V_Equation_00202_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00202 in V_Equation_00200.Elements("V_Equation_00202"))
                {
                    if (Current_V_Equation_00202.Attribute("Value").Value.ToString() == Formula_Variable_Number.ToString())
                    {
                        return Current_V_Equation_00202.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00202 in V_Equation_00200.Elements("V_Equation_00202"))
                {
                    if (Current_V_Equation_00202.Attribute("Value").Value.ToString() == Formula_Variable_Number.ToString())
                    {
                        Current_V_Equation_00202.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--variable is Input or Output-->
        public XElement V_Equation_00203
        {
            get
            {
                return V_Equation_00202.Element("V_Equation_00203");
            }
        }

        // <!--V_Equation_00203 value-->
        public string V_Equation_00203_Value
        {
            get
            {
                return V_Equation_00202.Element("V_Equation_00203").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00202.Element("V_Equation_00203").SetAttributeValue("Value", value);
            }
        }

        #endregion

        #region References

        // <!--References-->
        public XElement V_Equation_00300
        {
            get
            {
                foreach (XElement Current_V_Equation_00300 in Equation.Elements("V_Equation_00300"))
                {
                    if (Current_V_Equation_00300.Attribute("Value").Value.ToString() == Reference_Number.ToString())
                    {
                        return Current_V_Equation_00300;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00300 value-->
        public string V_Equation_00300_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00300 in Equation.Elements("V_Equation_00300"))
                {
                    if (Current_V_Equation_00300.Attribute("Value").Value.ToString() == Reference_Number.ToString())
                    {
                        return Current_V_Equation_00300.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00300 in Equation.Elements("V_Equation_00300"))
                {
                    if (Current_V_Equation_00300.Attribute("Value").Value.ToString() == Reference_Number.ToString())
                    {
                        Current_V_Equation_00300.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--Reference value-->
        public XElement V_Equation_00301
        {
            get
            {
                return V_Equation_00300.Element("V_Equation_00301");
            }
        }

        // <!--V_Equation_00301 value-->
        public string V_Equation_00301_Value
        {
            get
            {
                return V_Equation_00300.Element("V_Equation_00301").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00300.Element("V_Equation_00301").SetAttributeValue("Value", value);
            }
        }

        #endregion

        #region Cite as

        // <!--Cite as-->
        public XElement V_Equation_00400
        {
            get
            {
                foreach (XElement Current_V_Equation_00400 in Equation.Elements("V_Equation_00400"))
                {
                    if (Current_V_Equation_00400.Attribute("Value").Value.ToString() == Citation_Number.ToString())
                    {
                        return Current_V_Equation_00400;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00400 value-->
        public string V_Equation_00400_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00400 in Equation.Elements("V_Equation_00400"))
                {
                    if (Current_V_Equation_00400.Attribute("Value").Value.ToString() == Citation_Number.ToString())
                    {
                        return Current_V_Equation_00400.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00400 in Equation.Elements("V_Equation_00400"))
                {
                    if (Current_V_Equation_00400.Attribute("Value").Value.ToString() == Citation_Number.ToString())
                    {
                        Current_V_Equation_00400.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--Cite as value-->
        public XElement V_Equation_00401
        {
            get
            {
                return V_Equation_00400.Element("V_Equation_00401");
            }
        }

        // <!--V_Equation_00401 value-->
        public string V_Equation_00401_Value
        {
            get
            {
                return V_Equation_00400.Element("V_Equation_00401").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00400.Element("V_Equation_00401").SetAttributeValue("Value", value);
            }
        }

        #endregion

        #region Equation decription

        // <!--Equation description-->
        public XElement V_Equation_00500
        {
            get
            {
                foreach (XElement Current_V_Equation_00500 in Equation.Elements("V_Equation_00500"))
                {
                    if (Current_V_Equation_00500.Attribute("Value").Value.ToString() == Equation_Description_Number.ToString())
                    {
                        return Current_V_Equation_00500;
                    }
                }
                return null;
            }
        }

        // <!--V_Equation_00500 value-->
        public string V_Equation_00500_Value
        {
            get
            {
                foreach (XElement Current_V_Equation_00500 in Equation.Elements("V_Equation_00500"))
                {
                    if (Current_V_Equation_00500.Attribute("Value").Value.ToString() == Equation_Description_Number.ToString())
                    {
                        return Current_V_Equation_00500.Attribute("Value").Value;
                    }
                }
                return null;
            }
            set
            {
                foreach (XElement Current_V_Equation_00500 in Equation.Elements("V_Equation_00500"))
                {
                    if (Current_V_Equation_00500.Attribute("Value").Value.ToString() == Equation_Description_Number.ToString())
                    {
                        Current_V_Equation_00500.SetAttributeValue("Value", value);
                    }
                }
            }
        }

        // <!--English equation description value-->
        public XElement V_Equation_00501
        {
            get
            {
                return V_Equation_00500.Element("V_Equation_00501");
            }
        }

        // <!--V_Equation_00501 value-->
        public string V_Equation_00501_Value
        {
            get
            {
                return V_Equation_00500.Element("V_Equation_00501").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00500.Element("V_Equation_00501").SetAttributeValue("Value", value);
            }
        }

        // <!--German equation description value-->
        public XElement V_Equation_00502
        {
            get
            {
                return V_Equation_00500.Element("V_Equation_00502");
            }
        }

        // <!--V_Equation_00502 value-->
        public string V_Equation_00502_Value
        {
            get
            {
                return V_Equation_00500.Element("V_Equation_00502").Attribute("Value").Value;
            }
            set
            {
                V_Equation_00500.Element("V_Equation_00502").SetAttributeValue("Value", value);
            }
        }

        // List Name langauges.
        public List<string[]> GetDescriptionLanguages(XElement v_equation_00500)
        {
            List<string[]> langs = new List<string[]>();

            foreach (var desc in v_equation_00500.Descendants())
            {
                string[] lang = { desc.Name.LocalName, desc.FirstAttribute.Value };
                langs.Add(lang);
            }

            return langs;
        }

        #endregion

        #endregion
    }
}
